class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> ps;
        for(int i=0,j=0;i<pushed.size();i++)
        {
            ps.push(pushed[i]);
            while(!ps.empty() && ps.top()==popped[j]){
                j++;
                ps.pop();
            }
        }
        return ps.empty();
    }
};
